using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Arkanoid
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont fonte;
        Random random = new Random();
        EstadoJogo estadoJogo;
        int bolasNoJogo = 1;
        int score = 0;
        TimeSpan tempoGameOver = TimeSpan.Zero;
        TimeSpan tempoJogo = new TimeSpan(0, 2, 0);

        Texture2D texturaTeste;

        Barra barra;
        Texture2D texturaBarra;

        List<Bola> bolas = new List<Bola>();
        Texture2D texturaBola;
        bool bolaParada = true;
        bool multball = false;

        List<Alvo> alvos = new List<Alvo>();
        //Alvo alvo;
        Texture2D texturaAlvo;

        List<Upgrades> upgrades = new List<Upgrades>();
        int addUpgrade = 0;
        int numeroUpgrade = 0;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 900;
            graphics.PreferredBackBufferHeight = 600;
        }

        protected override void Initialize()
        {
            base.Initialize();

            estadoJogo = EstadoJogo.TITULO;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            fonte = Content.Load<SpriteFont>("fonte");

            texturaTeste = Content.Load<Texture2D>("teste");

            texturaBarra = Content.Load<Texture2D>("Imagens/barra");
            barra = new Barra(graphics, texturaBarra, new Vector2(graphics.PreferredBackBufferWidth / 2 - texturaBarra.Width / 2, 500));

            texturaBola = Content.Load<Texture2D>("Imagens/bola");
            
            texturaAlvo = Content.Load<Texture2D>("Imagens/alvo");
        }

        protected override void UnloadContent()
        {
            
        }

        public void Inicio()
        {
            bolas.Add( new Bola(graphics, texturaBola, Vector2.Zero, Vector2.Zero));
            bolasNoJogo = 1;
            foreach (Bola bola in bolas)
            {
                bola.posicao = new Vector2((barra.posicao.X + texturaBarra.Width / 2) - (texturaBola.Width / 2), barra.posicao.Y - texturaBola.Height);
            }
            bolaParada = true;
            AddAlvos();
            barra.posicao = new Vector2(graphics.PreferredBackBufferWidth / 2 - texturaBarra.Width / 2, 500);

            score = 0;
            tempoJogo = new TimeSpan(0, 2, 0);
        }

        public void AddAlvos()
        {
            for (int l = 0; l < 5; l++)
            {
                for (int c = 0; c < 15; c++)
                {
                    alvos.Add(new Alvo(graphics, texturaAlvo, new Vector2(65 + (texturaAlvo.Width + 5) * c, 50 + (texturaAlvo.Height + 10) * l)));
                }
            }
        }

        public void Colisao()
        {
            for (int b = 0; b < bolas.Count; b++)
            {
                if (bolas[b].retangulo.Intersects(barra.retangulo) && bolas[b].direcao.Y > 0)
                {
                    bolas[b].direcao.Y *= -1;
                    bolas[b].direcao.X += barra.efeito;
                }

                foreach (Upgrades up in upgrades)
                {
                    if (barra.retangulo.Intersects(up.retangulo) && up.ativo)
                    {
                        if (up.upgrade == 1 && up.ativo)
                        {
                            multball = true;
                            bolasNoJogo += 1;
                        }

                        if (up.upgrade == 2)
                        {
                            foreach (Bola bl in bolas)
                            {
                                bl.bolaPerfurar = true;
                                bl.tempoUpgrade = TimeSpan.Zero;
                            }  
                        }

                        if (up.upgrade == 3)
                        {
                            barra.aumentarBarra = true;
                            barra.tempoUpgrade = TimeSpan.Zero;
                        }
                        up.ativo = false;
                    }
                }

                for (int i = 0; i < alvos.Count; i++)
                {
                    if (bolas[b].retangulo.Intersects(alvos[i].retangulo) && alvos[i].ativo)
                    {

                        if (bolas[b].posicao.X + 14 >= alvos[i].posicao.X && bolas[b].posicao.X <= alvos[i].posicao.X + 35 && bolas[b].posicao.Y >= alvos[i].posicao.Y && bolas[b].direcao.Y < 0 || bolas[b].posicao.Y <= alvos[i].posicao.Y && bolas[b].direcao.Y > 0 && bolas[b].posicao.X + 14 >= alvos[i].posicao.X && bolas[b].posicao.X <= alvos[i].posicao.X + 35)
                        {
                            if (bolas[b].bolaPerfurar == false)
                            {
                                bolas[b].direcao.Y *= -1;
                            } 
                        }

                        if (bolas[b].posicao.X > alvos[i].posicao.X + 35 && bolas[b].direcao.X < 0 || bolas[b].posicao.X + 14 < alvos[i].posicao.X && bolas[b].direcao.X > 0)
                        {
                            if (bolas[b].bolaPerfurar == false)
                            {
                                bolas[b].direcao.X *= -1;
                            }
                        }

                        alvos[i].ativo = false;

                        score += 100;

                        addUpgrade = random.Next(0, 10);

                        if (addUpgrade == 9)
                        {
                            numeroUpgrade = random.Next(1, 4);
                            if (numeroUpgrade == 1)
                            {
                                upgrades.Add(new Upgrades(graphics, Content.Load<Texture2D>("Imagens/multBall"), new Vector2((int)alvos[i].posicao.X + 3, alvos[i].posicao.Y - 6), 1));
                            }

                            if (numeroUpgrade == 2)
                            {
                                upgrades.Add(new Upgrades(graphics, Content.Load<Texture2D>("Imagens/perfurar"), new Vector2((int)alvos[i].posicao.X + 3, alvos[i].posicao.Y - 6), 2));
                            }

                            if (numeroUpgrade == 3)
                            {
                                upgrades.Add(new Upgrades(graphics, Content.Load<Texture2D>("Imagens/barralonga"), new Vector2((int)alvos[i].posicao.X + 3, alvos[i].posicao.Y - 6), 3));
                            }

                            addUpgrade = 0;
                            numeroUpgrade = 0;
                        }
                    }
                }
            }   
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            KeyboardState teclado = Keyboard.GetState();

            switch (estadoJogo)
            {
                case EstadoJogo.TITULO:
                    bolasNoJogo = 1;
                    if (teclado.IsKeyDown(Keys.Enter))
                    {
                        Inicio();
                        estadoJogo = EstadoJogo.JOGO;
                    }
                    break;

                case EstadoJogo.JOGO:
                     barra.Update(gameTime);
                     Colisao();

                     tempoJogo -= gameTime.ElapsedGameTime;
                     if (tempoJogo <= TimeSpan.Zero)
                     {
                         estadoJogo = EstadoJogo.GAMEOVER;
                     }

            foreach (Upgrades up in upgrades)
            {
                up.Update();
            }

            for (int i = 0; i < bolas.Count; i++)
            {
                bolas[i].Update(gameTime);
                if (multball)
                {
                    bolas.Add(new Bola(graphics, texturaBola, new Vector2((barra.posicao.X + texturaBarra.Width / 2) - (texturaBola.Width / 2), barra.posicao.Y - texturaBola.Height), new Vector2(random.Next(-3, +3), -7)));
                    multball = false;
                }

                if (bolas[i].posicao.Y > 570 && bolas[i].ativo == true)
                {
                    bolasNoJogo -= 1;
                    bolas[i].ativo = false;
                }
            }

            if (bolasNoJogo <= 0)
            {
                estadoJogo = EstadoJogo.GAMEOVER;
            }

            if (teclado.IsKeyDown(Keys.Space) && bolaParada == true)
            {
                foreach (Bola bola in bolas)
                {
                    bola.direcao = new Vector2(random.Next(-3, +3), -7);
                }
                bolaParada = false;
            }

            if (bolaParada == true)
            {
                foreach (Bola bola in bolas)
                {
                    bola.posicao = new Vector2((barra.posicao.X + texturaBarra.Width / 2) - (texturaBola.Width / 2), barra.posicao.Y - texturaBola.Height);
                }
            }

                    break;

                case EstadoJogo.GAMEOVER:

                    foreach (Bola bola in bolas)
                    {
                        bola.ativo = false;
                    }

                    foreach (Alvo alvo in alvos)
                    {
                        alvo.ativo = false;
                    }

                    foreach (Upgrades up in upgrades)
                    {
                        up.ativo = false;
                    }

                    barra.aumentarBarra = false;
                    barra.tempoUpgrade = TimeSpan.Zero;

                    tempoGameOver += gameTime.ElapsedGameTime;

                    if (tempoGameOver.Seconds >= 5)
                    {
                        tempoGameOver = TimeSpan.Zero;
                        estadoJogo = EstadoJogo.TITULO;
                    }
                    break;
            }  
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();

            switch (estadoJogo)
            {
                case EstadoJogo.TITULO:
                    spriteBatch.Draw(Content.Load<Texture2D>("imagens/titulo"), new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);
                    break;
                
                case EstadoJogo.JOGO:
                    //spriteBatch.Draw(texturaTeste, multBall.retangulo, Color.White);
                    spriteBatch.DrawString(fonte, score.ToString(), Vector2.Zero, Color.White);
                    spriteBatch.DrawString(fonte, tempoJogo.Minutes + ":" + tempoJogo.Seconds, new Vector2(600, 0), Color.White);

                    barra.Draw(spriteBatch);
                    foreach (Bola bl in bolas)
                    {
                        bl.Draw(spriteBatch);
                    }

                    foreach (Alvo alvo in alvos)
                    {
                        alvo.Draw(spriteBatch);
                    }

                    foreach (Upgrades up in upgrades)
                    {
                        up.Draw(spriteBatch);
                    }

                    break;

                case EstadoJogo.GAMEOVER:
                    spriteBatch.Draw(Content.Load<Texture2D>("Imagens/game_over"), new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);

                    break;
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
